/***********************************************
 * Title:       StudentManageAction.java
 * Description: StudentManageAction.java
 * Author:      Niyuzhe
 * Create Date: 2009-12-20
 * CopyRight:   CopyRight(c)@2009
 * Company:     TJUSCS
 * Version:     1.0
 ***********************************************
 */
package edu.tju.student;

import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.infohazard.maverick.flow.ControllerContext;

import com.younker.waf.maverick.DispatchCtl;

import edu.tju.classes.ITheClassDao;
import edu.tju.classes.IbatisTheClassDao;
import edu.tju.education.ISpecialityDao;
import edu.tju.education.IbatisSpecialityDao;
import edu.tju.education.Speciality;
import edu.tju.model.IStudentDao;
import edu.tju.model.IbatisStudentDao;
import edu.tju.model.Student;
import edu.tju.model.TheClass;

/**
 * @author Niyuzhe
 *
 */
public class StudentBasicAction extends DispatchCtl
{
        
        private IStudentDao studentDao = new IbatisStudentDao();
        private Logger log = Logger.getLogger(this.getClass());
        private static final String SUCCESS_GET_SI="success_get_si";
        private static final String TEM="tem";
        private static final String BUF="buf";
        private static final String SEX="sex";


		/* (non-Javadoc)
         * @see org.infohazard.maverick.ctl.FormBeanUser#makeFormBean(org.infohazard.maverick.flow.ControllerContext)
         */
        @Override
        protected Object makeFormBean(ControllerContext arg0)
        {
                return new Student();
        }
        
        public String display( Object beans, ControllerContext ctx )
        {
                Student student = (Student)beans;
                try
                {
                        student = studentDao.getStudentById(student.getId());
                } catch (SQLException e)
                {
                        log.error("get student error[id=" + student.getId() + "].");
                        ctx.getRequest().setAttribute(RET_CODE, RC_FAILURE);
                        return SUCCESS;
                }
                
                ctx.setModel(student);
                
                return SUCCESS;
        }
        
        
        public String saveOrUpdate( Object beans, ControllerContext ctx )
        {
                Student student = (Student)beans;
                
                if( student.getId() == 0 )//save
                {
                        try
                        {
                                studentDao.insertStudent(student);
                        } catch (SQLException e)
                        {
                                log.error("Insert student error." + student, e);
                                ctx.getRequest().setAttribute(RET_CODE, RC_FAILURE);
                                return SUCCESS;
                        }
                }
                else
                {
                        try
                        {
                                studentDao.updateStudent(student);
                        } catch (SQLException e)
                        {
                                log.error("Update student error" + student, e);
                                ctx.getRequest().setAttribute(RET_CODE, RC_FAILURE);
                                return SUCCESS;
                        }
                }
                
                ctx.getRequest().setAttribute(RET_CODE, RC_SUCCESS);
                return SUCCESS;
        }

}
